// 统一封装接口方法
// 每个方法负责请求一个url地址
// 逻辑页面，导入这个接口方法，就能发请求
// 好处：统一管理


import request from "@/utils/request.js";
import { getToken } from "@/utils/token.js"


// 登录接口
// axios内部，会把参数对象转成json字符串格式发后台
// axios内部：会自动携带请求参数（headers）里
export const loginAPI = ({ mobile, code }) => request({
    url: '/v1_0/authorizations',
    method: 'POST',
    // data里面放请求体
    data: {
        mobile,
        code
    }
})

// 获取所有频道的接口数据
export const getAllChannelsApi = () => request({
    url: '/v1_0/channels',
    method: 'GET'
})


//频道-获取用户选择的频道
// 注意：用户没有登录，默认返回后台设置的默认频道列表
export const getUserChannelsAPI = () => request({
    url: '/v1_0/user/channels',
    method: 'GET',
    headers: {
        Authorization: `Bearer ${getToken()}`
    }
})


// 频道--更新覆盖频道
export const updateChannelsAPI = ({ channels }) => request({
    url: '/v1_0/user/channels',
    method: 'PUT',
    data: {
        channels //用户已选的整个频道数组
    },
    headers: {
        Authorization: `Bearer ${getToken()}`
    }
})

// 频道-删除用户指定的频道
export const removeTheChannelAPI = ({ channelId }) => request({
    url: `/v1_0/user/channels/${channelId}`,
    method: 'DELETE',
    headers: {
        Authorization: `Bearer ${getToken()}`
    }
})



// 文章 - 获取列表
export const getAllArticleListAPI = ({ channel_id, timestamp }) => request({
    url: '/v1_0/articles',
    headers: {
        Authorization: `Bearer ${getToken()}`
    },
    params: {
        // 这里的参数，axios会帮拼接在URL?后面（查询字符串）
        channel_id,
        timestamp
    }
})

//文章反馈-不感兴趣
export const dislikeArticleAPI = ({ artId }) => request({
    url: '/v1_0/article/dislikes',
    method: 'POST',
    headers: {
        Authorization: `Bearer ${getToken()}`
    },
    data: {
        target: artId
    }
})

//文章反馈-反馈垃圾内容
export const reportArticleAPI = ({ artId, type }) => request({
    url: '/v1_0/article/reports',
    method: 'POST',
    headers: {
        Authorization: `Bearer ${getToken()}`
    },
    data: {
        target: artId,
        type: type,
        remark: '无'
    }
})


//文章-获取详情
export const detailAPI = ({ artId }) => request({
    url: `/v1_0/articles/${artId}`
})

// 文章关注作者
export const userFollowedAPI = ({ userId }) => request({
    url: '/v1_0/user/followings',
    method: 'POST',
    headers: {
        Authorization: `Bearer ${getToken()}`
    },
    data: { target: userId }
})

// 文章取关作者
export const userUnFollowedAPI = ({ userId }) => request({
    url: `/v1_0/user/followings/${userId}`,
    method: 'DELETE',
    headers: {
        Authorization: `Bearer ${getToken()}`
    }
})


// 用户个人资料
export const userProfileAPI = () => request({
    url: '/v1_0/user/profile',
    headers: {
        Authorization: `Bearer ${getToken()}`
    },
})

// 用户更新用户基本信息
export const updateUserProfileAPI = ({ birthday, name }) => request({
    url: '/v1_0/user/profile',
    method: 'PATCH', //局部更新 PUT（全部更新）
    headers: {
        Authorization: `Bearer ${getToken()}`
    },
    data: {
        birthday,
        name,
    }
})



// 用户基本信息(我的页面显示数据)
export const userAPI = () => request({
    url: '/v1_0/user',
    headers: {
        Authorization: `Bearer ${getToken()}`
    },
})

// 用户更新头像接口
export const updateUserPhotoAPI = (fd) => request({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    headers: {
        Authorization: `Bearer ${getToken()}`
    },
    data: fd, //fd是一会外面传进来的new FormData()表单对象
})



// 用户-刷新token
export const getNewTokenAPI = () => request({
    url: '/v1_0/authorizations',
    method: 'PUT',
    headers: {
        Authorization: 'Bearer ' + localStorage.getItem('refresh_token')
    }
})




//文章点赞
export const likeArticleAPI = ({ articleId }) => request({
    url: '/v1_0/article/likings',
    method: 'POST',
    headers: {
        Authorization: `Bearer ${getToken()}`
    },
    data: {
        target: articleId
    }
})

// 文章取消点赞
export const UnlikeArticleAPI = ({ articleId }) => request({
    url: `/v1_0/article/likings/${articleId}`,
    method: 'DELETE',
    headers: {
        Authorization: `Bearer ${getToken()}`
    },

})


//获取评论列表接口
export const commentsListAPI = ({ id, offset = null, limit = 10 }) => request({
    url: '/v1_0/comments',
    headers: {
        Authorization: `Bearer ${getToken()}`
    },
    params: {
        type: 'a', //什么时候需要外面传：此值会变化
        source: id,
        offset,
        limit
    }
})


// 评论点赞
export const commentLikeingAPI = ({ commentId }) => request({
    url: '/v1_0/comment/likings',
    method: 'POST',
    headers: {
        Authorization: `Bearer ${getToken()}`
    },
    data: {
        target: commentId
    }
})

// 取消评论点赞
export const commentUnLikeingAPI = ({ commentId }) => request({
    url: `/v1_0/comment/likings/${commentId}`,
    method: 'DELETE',
    headers: {
        Authorization: `Bearer ${getToken()}`
    },

})

// 文章-评论发布评论
export const commentSendAPI = ({ commentId, content }) => request({
    url: '/v1_0/comments',
    method: 'POST',
    headers: {
        Authorization: `Bearer ${getToken()}`
    },
    data: {
        target: commentId,
        content
    }
})





// 搜索联想菜单列表接口
export const suggestListAPI = ({ keywords }) => request({
    url: '/v1_0/suggestion',
    method: 'GET',
    params: { q: keywords }
})


// 搜索结果列表
export const searchResultListAPI = ({ q, page = 1 }) => request({
    url: '/v1_0/search',
    params: {
        q,
        page
    }
})